package com.atguigu.eduService.controller;


import com.atguigu.common.Result;
import com.atguigu.eduService.client.VodClient;
import com.atguigu.eduService.entity.EduVideo;
import com.atguigu.eduService.service.EduVideoService;
import com.mysql.cj.protocol.x.ReusableInputStream;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-07-27
 */
@RestController
@RequestMapping("/edu/content/video")
//@CrossOrigin
public class EduVideoController {

    @Resource
    private EduVideoService videoService;

    /**
     * 注入VodClient
     */
    @Resource
    private VodClient vodClient;

    @ApiOperation(value = "添加小节")
    @PostMapping
    public Result add(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return Result.ok();
    }

    @ApiOperation(value = "删除小节")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        //根据小节id获取视频id
        EduVideo video = videoService.getById(id);
        if(!StringUtils.isEmpty(video.getVideoSourceId())){
            vodClient.removeVideo(video.getVideoSourceId());
        }
        videoService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "修改小节")
    @PutMapping
    public Result update(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return Result.ok();
    }

    @ApiOperation(value = "根据小节id获取小节信息")
    @GetMapping("/{videoId}")
    public Result get(@PathVariable("videoId") String id){
        EduVideo video = videoService.getById(id);
        return Result.ok().data("video",video);

    }
}

